package com.team9.fitness.service;

import com.team9.fitness.entity.ExerciseInfo;
import com.team9.fitness.mapper.ExerciseInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 运动信息服务
 */
@Service
@Transactional
@Slf4j
public class ExerciseInfoService {

    @Autowired
    private ExerciseInfoMapper exerciseInfoMapper;

    /**
     * 创建运动信息
     */
    public ExerciseInfo createExerciseInfo(ExerciseInfo exerciseInfo) {
        exerciseInfo.setCreatedAtIfNull();
        exerciseInfo.setUpdatedAtIfNull();
        exerciseInfoMapper.insert(exerciseInfo);
        return exerciseInfo;
    }

    /**
     * 根据ID获取运动信息
     */
    public ExerciseInfo getExerciseInfoById(Long id) {
        return exerciseInfoMapper.selectById(id);
    }

    /**
     * 获取所有运动信息
     */
    public List<ExerciseInfo> getAllExerciseInfo() {
        return exerciseInfoMapper.selectAll();
    }

    /**
     * 根据分类获取运动信息
     */
    public List<ExerciseInfo> getExerciseInfoByCategory(String category) {
        return exerciseInfoMapper.selectByCategory(category);
    }

    /**
     * 根据名称搜索运动信息
     */
    public List<ExerciseInfo> searchExerciseInfoByName(String keyword) {
        return exerciseInfoMapper.searchByName(keyword);
    }

    /**
     * 获取所有运动分类
     */
    public List<String> getAllCategories() {
        return exerciseInfoMapper.selectAllCategories();
    }

    /**
     * 更新运动信息
     */
    public ExerciseInfo updateExerciseInfo(ExerciseInfo exerciseInfo) {
        exerciseInfo.setUpdatedAtIfNull();
        exerciseInfoMapper.update(exerciseInfo);
        return exerciseInfo;
    }

    /**
     * 删除运动信息
     */
    public void deleteExerciseInfo(Long id) {
        exerciseInfoMapper.deleteById(id);
    }
}

